//
//  ViewController.m
//  XSSocketDemo
//
//  Created by xingshuai on 2021/8/12.
//

#import "ViewController.h"

@interface ViewController () <XSSocketRocketToolDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = UIColor.orangeColor;
    
    // 初始化socket
    //[self creatSocketData];
    
}

// socket
-(void)creatSocketData {
    // 直播间socket地址拼接
    NSString *sockAddress = @"";
    // 直播间id (socket心跳定时器发消息参数，不需要可以不传)
    [XSSocketManager sharedInstance].liveId = @"";
    // 服务器socket地址
    [XSSocketManager sharedInstance].wsURLString = sockAddress;
    // socket方法代理方法
    [XSSocketManager sharedInstance].delegate = self;
    // 连接socket
    [[XSSocketManager sharedInstance] connect];
}

// webSocket已经打开
- (void)webSocketDidOpen:(SRWebSocket *_Nullable)webSocket {
    NSLog(@"已经打开socket");
}

// 服务器心跳回调
- (void)webSocket:(SRWebSocket *_Nullable)webSocket didReceivePong:(nullable NSData *)pongData {
    NSLog(@"服务器端socket还在");
}

// 收到连接关闭的回调
- (void)webSocket:(SRWebSocket *_Nullable)webSocket didCloseWithCode:(NSInteger)code reason:(nullable NSString *)reason wasClean:(BOOL)wasClean {
    NSLog(@"socket连线关闭，这里处理重现链接逻辑");
    /**
    // 记录socket已断开
    self.isNetworkDisconnect = NO;
    // 如果还在当前页面在重新连接
    if (self.isCurrentView) {
        // 有网在重连/否则关闭
        if ([XAClient sharedClient].isNetWorkAvailable) {
            [[XSSocketManager sharedInstance] performSelector:@selector(reconnect) withObject:nil afterDelay:1];
        } else {
            [[XSSocketManager sharedInstance] closeWebSocket];
        }
    }
     */
}

// 收到连接错误的回调
- (void)webSocket:(SRWebSocket *_Nullable)webSocket didFailWithError:(NSError *_Nullable)error {
    NSLog(@"socket连线断开，这里处理重现链接逻辑");
    /**
    // 记录socket已断开
    self.isNetworkDisconnect = NO;
    // 如果还在当前页面在重新连接
    if (self.isCurrentView) {
        // 有网在重连/否则关闭
        if ([XAClient sharedClient].isNetWorkAvailable) {
            [[XSSocketManager sharedInstance] performSelector:@selector(reconnect) withObject:nil afterDelay:1];
        } else {
            [[XSSocketManager sharedInstance] closeWebSocket];
        }
    }
     */
}


// 收到socket回调消息展示处理
- (void)webSocket:(SRWebSocket *_Nullable)webSocket didReceiveMessage:(id _Nullable )message {
    NSLog(@"收到socket回调消息展示处理");
    /**
    // 如：返回json类型字符串处理（这里需要根据收到的消息类型做不同的处理）
    // 判断是否字符串
    if ([NSString trimEmpty:message] && ![message isKindOfClass:[NSString class]]) {
        NSLog(@"不是字符串");
        return;
    }
    // 判断是否字符串类型json
    if (![message hasPrefix:@"{"] || ![message hasSuffix:@"}"]) {
        NSLog(@"不是字符串类型json");
        return;
    }
    // 字符串类型json转字典
    NSDictionary *messDic = [self dictionaryWithJsonString:message];
     
    // 然后去赋值刷新列表展示
     */
}

// json字符串转字典
- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}


-(void)dealloc {
    // 关闭socket (或者放到页面消失以后的方法中关闭也行)
    [[XSSocketManager sharedInstance] closeWebSocket];
}


@end
